﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.IO.IsolatedStorage;
using System.Xml.Linq;
using System.Linq;
using System.IO;
using System.Collections;
using System.Collections.Generic;
namespace ActivityTracker.Library
{
    public class Data
    {
        #region Global

        IsolatedStorageFile isoStore = IsolatedStorageFile.GetUserStoreForApplication();
        IsolatedStorageFileStream isoStream;
        XDocument xdoc;
       

        #endregion
      
        public void CreateFileDirectory()
        {
            try
            {
                if (!isoStore.DirectoryExists("ActivityTracker"))
                    isoStore.CreateDirectory("ActivityTracker");
            }
            catch (Exception ex)
            {
                Exception exNew = new Exception("Exception in CreateFileDirectory Method In ActivityTracker.Library.Activity.Actvity file\n\n" + ex.Message + " \n\n Stack Trace \n" + ex.StackTrace);
                throw exNew;
            }
        }

        public void SaveDefaultActivities()
        {
            try
            {
                if (!isoStore.FileExists("ActivityTracker/HelpMenu.xml"))
                {
                    Uri uri = new Uri("DefaultData/HelpMenu.xml", UriKind.Relative);
                    xdoc = XDocument.Load(uri.ToString());
                    isoStream = new IsolatedStorageFileStream("ActivityTracker/HelpMenu.xml", FileMode.Create, isoStore);
                    xdoc.Save(isoStream);
                    isoStream.Close();
                }
            }
            catch (Exception ex)
            {
                Exception exNew = new Exception("Exception in SaveDefaultActivities Method In ActivityTracker.Library.Activity.Actvity file\n\n" + ex.Message + " \n\n Stack Trace \n" + ex.StackTrace);
                throw exNew;
            }
        }
        public List<MenuProperties> LoadMenuList()
        {
            List<MenuProperties> objMenulist = new List<MenuProperties>();
            try
            {

                if (isoStore.FileExists("ActivityTracker/HelpMenu.xml"))
                {
                    isoStream = new IsolatedStorageFileStream("ActivityTracker/HelpMenu.xml", FileMode.Open, isoStore);
                    xdoc = XDocument.Load(isoStream);
                    isoStream.Close();


                    var xquery = from v in xdoc.Descendants("menu") select v;
                    foreach (var item in xquery)
                    {

                        MenuProperties disprop = new MenuProperties();
                        disprop.Id = item.Attribute("id").Value;
                        //disprop.Image = "/" + App.ImagePath + it.Element("i").Value;
                        disprop.Name = item.Element("name").Value;
                        disprop.Url = item.Element("url").Value;
                        objMenulist.Add(disprop);
                    }
                }
            }

            catch (Exception ex)
            {
                Exception exNew = new Exception("Exception in DeleteSchedulesBasedOnActId Method In ActivityTracker.Library.Schedules.Schedules file\n\n" + ex.Message + " \n\n Stack Trace \n" + ex.StackTrace);
               

            }
            return objMenulist;
        }

    }
}
